<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color: red;
    }
    .news .router-link-exact-active{
      background-color: rgb(236, 224, 49);
      color: rgb(146, 221, 24);
    }
  </style>
</head>
<body>
  <div id="app">
    <router-link  to="/home">
      首页
    </router-link>
    <router-link to="/news">新闻页</router-link>
    <router-link replace to="/about">关于我们页</router-link>
    <!-- 视图上 定义 router-view组件 是路由出口 router-view定义在哪里路由组件就在哪里渲染 -->
    <router-view></router-view>
  </div>
  <script src="./vue.global.js"></script>
  <script src="./vue-router.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp();
    // 1准备路由组件 config
    const HomePage = {
      template: `
        <div>
          <h2>首页</h2>
        </div>
      `
    }
    const NewsPage = {
      template: `
        <div class="news">
          <h2>新闻页</h2>
          <router-link to="/news/native">国内新闻</router-link>
          <router-link replace to="/news/abroad">国外新闻</router-link>
          <hr/>
          <router-view></router-view>
        </div>
      `
    }
    const NativeNews = {
      template: `
        <div>
          <h3>国内新闻</h3>
        </div>
      `
    }
    const AbroadNews = {
      template: `
        <div>
          <h3>国外新闻</h3>
        </div>
      `
    }
    const AboutPage = {
      template: `
        <div>
          <h2>关于我们页</h2>
        </div>
      `
    }
    // 定义404组件
    const NotFound = {
      template: `
        <div>
          <h2>呕吼404啦</h2>
        </div>
        `
    }
    // 2创建路由实例
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        component: HomePage
      },
      {
        path: '/news',
        name: 'news',
        component: NewsPage,
        // 子路由嵌套在父级路由规则 children属性中
        children: [
          {
            path: '/news',
            redirect: '/news/native'
          },
          {
            path: '/news/native',
            name: 'nativeNews',
            component: NativeNews
          },
          {
            path: '/news/abroad',
            name: 'abroadNews',
            component: AbroadNews
          }
        ]
      },
      {
        path: '/about',
        name: 'about',
        component: AboutPage
      },
      {
        path: '/:pathMatch(.*)*',
        component: NotFound
      }
    ]
    const router = VueRouter.createRouter({
      // 创建hash模式的 路由
      history: VueRouter.createWebHashHistory(),
      routes
    })
    // 3 将路由实例挂载  app应用实例上
    app.use(router);
    app.mount('#app')
  </script>
</body>
</html>